# -*- coding: utf-8 -*-
"""
Created on Mon Nov 16 10:29:21 2020

@author: xyz

存放给药方案的函数


"""

#必备的包
import sys
import os
from PyQt5.QtWidgets import QWidget, QApplication, QFileDialog, QMessageBox
from PyQt5.QtCore import Qt
from fit import Fit
#fit界面的其他子界面
from window import MainWin
#nlmixr的包
from nlmixr import *

class MedicinePlot(MainWin):
    def __init__(self):
        super().__init__()
        #用来存放需要绘制图形的变量名称
        self.plot_list_in_medicine = [] 
        #获取globalenv的按钮
        self.pushButton_7.clicked.connect(self.get_globalenv_in_medicine)
        self.pushButton_7.clicked.connect(self.display_globalenv_in_medicine)
        #获取选中变量的按钮 self.global_var_in_check是最终返回的被选中的var
        self.pushButton_9.clicked.connect(self.get_contents_in_lineEdit_3)
        #生成pdf
        self.pushButton_8.clicked.connect(self.wait_three_minute)
        self.pushButton_8.clicked.connect(self.plot_it_in_medicine)
        
    def get_globalenv_in_medicine(self):
        #获取globalenv中的变量
        glonames = robjects.r.ls(globalenv)
        self.globalnames = []
        for x in glonames:
            self.globalnames.append(x)
            
    def display_globalenv_in_medicine(self):
        #显示globalenv中的变量
        self.textBrowser_3.setText('')
        for x in self.globalnames:
            self.textBrowser_3.append(x)
            
    def get_contents_in_lineEdit_3(self):
        #获取从lineEdit_3的输入的内容
        self.global_var_in_medicine = self.lineEdit_3.text()
        QMessageBox.information(self,'提示','获取成功！',QMessageBox.Ok,QMessageBox.Ok)
        return self.global_var_in_medicine
    
    def plot_it_in_medicine(self):
        self.pushButton_8.setEnabled(False)
        robjects.r('''ev <- eventTable(amount.units="mg", time.units="hours") %>%
    add.dosing(dose=600, nbr.doses=10, dosing.interval=24) %>%
    add.dosing(dose=2000, nbr.doses=5,
               start.time=240,dosing.interval=48) %>%
    add.sampling(0:480);''')
        robjects.r('''library(gridExtra)
library(RxODE)
library(MASS)
library(data.table)
library(ggforce)
library(ggplot2)
library(nlmixr)
library(xpose)
library(xpose.nlmixr)
library(shinyMixR)
library(stats)''')
        # print("library loaded")
        robjects.r('''sim.%s <- simulate(%s, events=ev, nSub=500)''' %(self.global_var_in_medicine, self.global_var_in_medicine))
        print("sim ok")
        robjects.r('''sim.%s.unc <- simulate(%s, events=ev, nSub=500, nStud=100)''' %(self.global_var_in_medicine, self.global_var_in_medicine))
        # print('sim unc ok')
        # pdf("fig3_%s.pdf" %self.global_var_in_medicine, width=8,height=8)
        # print("pdf ok")
        # plot(sim_unc)
        # print("sim_unc ok")
        # dev_off()
        # print("dev_off ok")
        
        robjects.r('''pdf("fig3_%s.pdf",width=8,height=8)
print(plot(sim.%s.unc))
dev.off()'''%(self.global_var_in_medicine, self.global_var_in_medicine))
        self.pushButton_8.setEnabled(True)
        QMessageBox.information(self,'提示','已保存至当前工作目录下',QMessageBox.Ok,QMessageBox.Ok)
        
        
        
        
        
        
        
        
        
        
        
        
        